package com.sls.game.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.SuppliersManageDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.SuppliersInfo;
import com.sls.game.pojo.SuppliersManageParameter;
import com.sls.game.service.SuppliersManageService;

/**
 * 
 * @description 供应商管理
 * @author Wangxuegang
 * @date 2012-9-12
 * 
 */
@Service("suppliersManageService")
public class SuppliersManageServiceImpl implements SuppliersManageService {
	private static Logger log = Logger
			.getLogger(SuppliersManageServiceImpl.class);

	private Gson gson = null;

	public SuppliersManageServiceImpl() {
		gson = CommonUtil.gson();
	}

	/**
	 * 供应商管理DAO
	 */
	private SuppliersManageDAO suppliersManageDAO;

	public SuppliersManageDAO getSuppliersManageDAO() {
		return suppliersManageDAO;
	}

	@Resource(name = "suppliersManageDAO")
	public void setSuppliersManageDAO(SuppliersManageDAO suppliersManageDAO) {
		this.suppliersManageDAO = suppliersManageDAO;
	}

	/**
	 * 分页查询产品供应商
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryByPage(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		// 对消息body进行解析
		SuppliersManageParameter pageInruire = gson.fromJson(body,
				SuppliersManageParameter.class);

		boolean flag = checkDataAOfQueryByPage(pageInruire, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				Pagination<SuppliersInfo> paginationList = null;
				paginationList = suppliersManageDAO
						.queryPaginationList(pageInruire);
				String resultBody = null;
				if (paginationList != null) {
					resultBody = gson.toJson(paginationList);
				}

				log.info("分页查询结果为：  " + resultBody);
				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}
				return resultBody;
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 供应商新增
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String insert(String body, Head head) throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		// 对消息body进行解析
		SuppliersInfo suppliersInfo = gson.fromJson(body, SuppliersInfo.class);
		boolean flag = checkDataOfInsert(suppliersInfo, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				// 查询数据表
				String supplyIdStr = suppliersManageDAO.query();
				String supplyId = "";
				if ("".equals(supplyIdStr) || supplyIdStr == null) {
					supplyId = "20121000";
				} else {
					int value = Integer.parseInt(supplyIdStr) + 1;
					supplyId = value + "";
				}

				SuppliersInfo suppliersInfo1 = new SuppliersInfo();
				suppliersInfo1.setId(UUID.randomUUID().toString());
				suppliersInfo1.setSupplyId(supplyId);
				suppliersInfo1.setSupplyName(suppliersInfo.getSupplyName());
				// 0：不再合作，1：合作
				suppliersInfo1.setSupplyState(suppliersInfo.getSupplyState());
				suppliersInfo1.setRemark(suppliersInfo.getRemark());

				suppliersManageDAO.insert(suppliersInfo1);
				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}
				return null;
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 供应商修改
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String update(String body, Head head) throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		SuppliersInfo suppliersInfo = gson.fromJson(body, SuppliersInfo.class);
		boolean flag = checkDataOfUpdate(suppliersInfo, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				suppliersManageDAO.update(suppliersInfo);

				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}
				return null;
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 根据供应商ID（supplyId）查询供应商
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryBySupplyId(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		SuppliersInfo suppliersInfo = gson.fromJson(body, SuppliersInfo.class);
		boolean flag = checkDataOfQueryBySupplyId(suppliersInfo,
				head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				String supplyId = suppliersInfo.getSupplyId();
				SuppliersInfo suppliersInfo1 = suppliersManageDAO
						.queryBySupplyId(supplyId);
				String resultBody = null;
				if (suppliersInfo1 != null) {
					resultBody = gson.toJson(suppliersInfo1);
				}
				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}

				return resultBody;
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataAOfQueryByPage(
			SuppliersManageParameter pageInruire, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		// 当前页
		String pageIndex = pageInruire.getCurrentPage() + "";
		// 每页显示记录数
		String pageSize = pageInruire.getPageSize() + "";

		if ("".equals(pageIndex) || pageIndex == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.CURRENT_PAGE_NULL.getCode(),
					ExceptionCodeDes.CURRENT_PAGE_NULL.getDescription());
		} else if ("".equals(pageSize) || pageSize == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PAGE_SIZE_NULL.getCode(),
					ExceptionCodeDes.PAGE_SIZE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfInsert(SuppliersInfo suppliersInfo, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		// 供应商名称
		String supplyName = suppliersInfo.getSupplyName();
		// 供应商状态
		String supplyState = suppliersInfo.getSupplyState();

		if ("".equals(supplyName) || supplyName == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SUPPLY_NAME_NULL.getCode(),
					ExceptionCodeDes.SUPPLY_NAME_NULL.getDescription());
		} else if ("".equals(supplyState) || supplyState == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SUPPLY_STATUS_NULL.getCode(),
					ExceptionCodeDes.SUPPLY_STATUS_NULL.getDescription());
		} else {
			String id = this.suppliersManageDAO.queryBySupplyName(supplyName);
			if (StringUtils.isNotEmpty(id)) {
				throw new ServerDisponseException(
						ExceptionCodeDes.SUPPLY_EXIST.getCode(),
						ExceptionCodeDes.SUPPLY_EXIST.getDescription());
			} else {
				flag = true;
			}
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfUpdate(SuppliersInfo suppliersInfo, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		// 供应商编号
		String supplyId = suppliersInfo.getSupplyId();
		// 供应商状态
		String supplyState = suppliersInfo.getSupplyState();

		if ("".equals(supplyId) || supplyId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SUPPLY_ID_NULL.getCode(),
					ExceptionCodeDes.SUPPLY_ID_NULL.getDescription());
		} else if ("".equals(supplyState) || supplyState == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SUPPLY_STATUS_NULL.getCode(),
					ExceptionCodeDes.SUPPLY_STATUS_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfQueryBySupplyId(SuppliersInfo suppliersInfo,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		// 供应商编号
		String supplyId = suppliersInfo.getSupplyId();
		if ("".equals(supplyId) || supplyId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SUPPLY_ID_NULL.getCode(),
					ExceptionCodeDes.SUPPLY_ID_NULL.getDescription());
		} else {
			flag = true;
		}
		return flag;
	}

}
